package cn.magictodo.weblog.admin.event.subscriber.statistics;

import cn.magictodo.weblog.admin.event.UpdateArticleEvent;
import cn.magictodo.weblog.admin.service.AdminStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author liu
 * @since 2023-12-11 21:00
 */
@Component
@Slf4j
public class UpdateArticleStatisticsSubscriber implements ApplicationListener<UpdateArticleEvent> {
    @Autowired
    private AdminStatisticsService adminStatisticsService;

    @Override
    @Async("threadPoolTaskExecutor")
    public void onApplicationEvent(UpdateArticleEvent event) {
        try {
            Thread.sleep(5000);
            adminStatisticsService.statisticsCategoryArticleTotal();
            adminStatisticsService.statisticsTagArticleTotal();
            log.info("==> 修改文章，重新统计各分类、标签下文章总数");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
